package com.kinon.market.tools;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.kinon.market.data.UPacketFactory;
import com.kinon.market.entity.HeadBean;
import com.kinon.market.entity.ItemBean;
import com.kinon.market.entity.KVP;
import com.kinon.market.entity.ListingBean;
import com.kinon.market.view.MainActivity;

import java.util.ArrayList;
import java.util.List;

import sunmi.ds.DSKernel;
import sunmi.ds.callback.ISendCallback;
import sunmi.ds.data.DataPacket;

/**
 * 项目名称：DSC
 * 类描述：
 * 创建人：Abtswiath丶lxy
 * 创建时间：2016/10/18 16:34
 * 修改人：longx
 * 修改时间：2016/10/18 16:34
 * 修改备注：
 */
public class SendShopList {

    private static final String IMGS_ID = "lxy";
    List<ItemBean> items = new ArrayList<>();
    List<KVP> sm = new ArrayList<>();
    private SendShopList mInstance;
    private Context mContext;
    private DSKernel mDSKernel;
    private long taskId;
    private MainActivity activity;
    /**
     * 发送消息的回调
     */
    private ISendCallback callback = new ISendCallback() {

        @Override
        public void onSendFail(int arg0, String arg1) {
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    showToast("发送失败!");
                }
            });
        }

        @Override
        public void onSendProcess(long arg0, long arg1) {
            // TODO
        }

        @Override
        public void onSendSuccess(long arg0) {
            if (activity != null) {
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showToast("发送成功!");
                    }
                });
            }
        }

    };

    public SendShopList(Context context, DSKernel mDSKernel, List<ItemBean> items, List<KVP> sm) {
        this.mContext = context;
        this.mDSKernel = mDSKernel;
        this.items = items;
        this.sm = sm;
    }

    public SendShopList getInstance(Context context, DSKernel mDSKernel, List<ItemBean> items, List<KVP> sm) {
        if (mInstance == null) {
            mInstance = new SendShopList(context, mDSKernel, items, sm);
        }
        return mInstance;
    }

    public void send(boolean custom, String json) {
        String jsonStr = mockTextListData(true);
        if (custom) {
            jsonStr = json;
        }
        DataPacket dsPacket = UPacketFactory.buildShowText(DSKernel.getDSDPackageName(), jsonStr, callback);
        mDSKernel.sendData(dsPacket);
    }

    private void showToast(String msg) {
        Toast.makeText(activity.getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
    }

    public String mockTextListData(boolean isSingle) {
        ListingBean mListingBean = new ListingBean();
        // title
        mListingBean.setTitle("智慧食堂欢迎您");

        // head
        HeadBean mHeadBean = new HeadBean();
        mHeadBean.setParam1("商品编号");
        mHeadBean.setParam2("商品名");
        mHeadBean.setParam3("单价");

        if (isSingle) {
            mHeadBean.setParam4("数量");
            mHeadBean.setParam5("小计");
        }

        mListingBean.setHead(mHeadBean);

        // lists
//        List<ItemBean> items = new ArrayList<>();
//        for (int i = 0; i < items.size(); i++) {
//            ItemBean mItemBean = new ItemBean();
//            mItemBean.setParam1("" + (i + 1));
//            mItemBean.setParam2("可爱多" + i);
//            mItemBean.setParam3("20.50");
//
//            if (isSingle) {
//                mItemBean.setParam4("" + i + 1);
//                mItemBean.setParam5("41.00");
//            }
//
//            items.add(mItemBean);
//        }
        mListingBean.setList(items);
        Log.i("zq", "items--------" + items);

        // statement
//        List<KVP> sm = new ArrayList<>();

        int num;
        if (isSingle) {
            num = 3;
        } else {
            num = 7;
        }

//        for (int i = 0; i < num; i++) {
//            KVP KVPList = new KVP();
//            KVPList.setName("结算");
//            KVPList.setValue("40.00");
//            sm.add(KVPList);
//        }
//            KVP k1 = new KVP();
//            k1.setName("订单编号");
//            k1.setValue("40.00");
//            sm.add(k1);
//
//        KVP k2 = new KVP();
//        k2.setName("共计");
//        k2.setValue("40.00");
//        sm.add(k2);

//        KVP k3 = new KVP();
//        k3.setName("消费");
//        k3.setValue("40.00");
//        sm.add(k3);

        mListingBean.setKVPList(sm);

        String json = new Gson().toJson(mListingBean);

        return json;
    }

}
